<template>
  <page-header-wrapper>
    <page-tab-list> </page-tab-list>
    <a-card :border="false">
      <div class="table-operator" style="display: flex; justify-content: space-between">
        <a-radio-group v-model="queryParam.status" @change="getList(1)">
          <a-radio-button :value="-1"> 全部 </a-radio-button>
          <a-radio-button :value="1"> 已启用 </a-radio-button>
          <a-radio-button :value="0"> 已禁用 </a-radio-button>
        </a-radio-group>

        <div>
          <a-button type="primary" icon="plus" @click="showModal()">添加</a-button>
          <a-dropdown>
            <a-menu slot="overlay">
              <a-menu-item @click="handleAll(selectedRowKeys, 2)">批量删除</a-menu-item>
              <a-menu-item @click="handleAll(selectedRowKeys, 1)">批量开启</a-menu-item>
              <a-menu-item @click="handleAll(selectedRowKeys, 0)">批量禁用</a-menu-item>
            </a-menu>
            <a-button> 批量操作 </a-button>
          </a-dropdown>
        </div>
      </div>
      <a-table
        :rowKey="
          (r, i) => {
            return r.id
          }
        "
        :rowSelection="{ selectedRowKeys: this.selectedRowKeys, onChange: this.onSelectChange }"
        size="default"
        :dataSource="tabList"
        :pagination="{
          total: this.total,
          showSizeChanger: false,
          onChange: this.getList,
        }"
        :columns="columns"
        :scroll="{ x: 'max-content' }"
      >
        <a-avatar slot="cover_pic" slot-scope="cover_pic" :src="cover_pic" shape="square" :size="64" />

        <div slot="status" slot-scope="status">
          <a-badge :color="getStatus(status)[1]" :text="getStatus(status)[0]" />
        </div>
        <span slot="action" slot-scope="text, record">
          <a-button type="link" @click="showModal(record)">编辑</a-button>
          <a-divider type="vertical" />
          <a-dropdown>
            <a-menu slot="overlay">
              <a-menu-item>
                <a href="javascript:;" @click="handleAll(record.id, 1)">启用</a>
                <a href="javascript:;" @click="handleAll(record.id, 0)">禁用</a>
                <a href="javascript:;" @click="handleAll(record.id, 2)">删除</a>
              </a-menu-item>
            </a-menu>
            <a class="ant-dropdown-link"> 更多 <a-icon type="down" /> </a>
          </a-dropdown>
        </span>
      </a-table>
    </a-card>
    <a-modal
      :title="form.id ? '编辑轮播广告' : '新增轮播广告'"
      :visible="visible"
      :confirm-loading="confirmLoading"
      @ok="handleOk"
      @cancel="visible = false"
    >
      <a-form-model ref="form" :model="form" :label-col="{ span: 4 }" :wrapper-col="{ span: 20 }">
        <a-form-item prop="cover_pic" label="上传图片" required>
          <CyyUploadImg
            @uploadSuccess="(e) => (form.cover_pic = e.data)"
            @deleteImg="form.cover_pic = ''"
            :imgSrc="form.cover_pic"
            imgSizeText="702*240"
          >
          </CyyUploadImg>
        </a-form-item>
        <a-form-item prop="name" label="标题名称">
          <a-input v-model="form.name" placeholder="请输入"></a-input>
        </a-form-item>
        <a-form-item prop="url" label="广告链接">
          <div class="flex-y-center">
            <a-input disabled class="flex-w-1" :value="form.url.name ? form.url.name : ''" />
            <a-button class="btn" type="primary" @click="showLink = true">选择</a-button>
          </div>
        </a-form-item>
        <a-form-item prop="sort" label="轮播排序">
          <a-input-number v-model="form.sort" :precision="0" :min="1" :max="999" />
        </a-form-item>
        <a-form-item prop="status" label="是否启用">
          <a-radio-group v-model="form.status" name="radioGroup" :default-value="status">
            <a-radio :value="1"> 开启 </a-radio>
            <a-radio :value="0"> 关闭 </a-radio>
          </a-radio-group>
        </a-form-item>
      </a-form-model>
    </a-modal>

    <SelectLink v-if="showLink" @onOk="onOk" @cancel="showLink = false" :isSingle="true"></SelectLink>
  </page-header-wrapper>
</template>

<script>
const columns = [
  {
    title: 'ID',
    dataIndex: 'id',
  },
  {
    title: '图片',
    dataIndex: 'cover_pic',
    scopedSlots: {
      customRender: 'cover_pic',
    },
  },
  {
    title: '标题',
    dataIndex: 'name',
  },
  {
    title: '链接',
    dataIndex: 'url.name',
  },
  {
    title: '排序',
    dataIndex: 'sort',
    sorter: (a, b) => a.sort - b.sort,
  },
  {
    title: '创建时间',
    dataIndex: 'created_at',
    sorter: (a, b) => Number(new Date(a.created_at)) - Number(new Date(b.created_at)),
  },
  {
    title: '状态',
    dataIndex: 'status',
    filters: [
      { text: '禁用', value: 0 },
      { text: '启用', value: 1 },
    ],
    onFilter: (value, record) => record.status == value,
    scopedSlots: {
      customRender: 'status',
    },
  },
  {
    title: '操作',
    scopedSlots: {
      customRender: 'action',
    },
  },
]
import pt from '@/api/pt'
import { cyyUploadImg } from '@/components/CyyUi/index'
import SelectLink from '@/components/DiyNew/SelectLink/index.vue'

export default {
  components: {
    CyyUploadImg: cyyUploadImg,
    SelectLink,
  },
  data() {
    return {
      columns,
      tabList: [],
      visible: false,
      confirmLoading: false,
      queryParam: {
        name: '',
        start_time: '',
        end_time: '',
        status: -1,
        pageNo: 1,
        pageSize: 10,
      },
      form: {
        name: '',
        cover_pic: '',
        url: '',
        sort: '',
        status: 1,
      },
      total: 0,
      formRules: {
        cover_pic: [{ required: true, trigger: 'change', message: '图片不能为空' }],
      },
      selectedRowKeys: [],
      showLink: false,
      queryTime: [],
    }
  },
  computed: {
    getStatus() {
      return (status) => {
        let text, color
        switch (String(status)) {
          case '0':
            text = '禁用'
            color = '#ff0000'
            break

          case '1':
            text = '启用'
            color = '#65C817'
            break
        }
        return [text, color]
      }
    },
  },
  created() {
    this.getList()
  },
  methods: {
    changeTime(e) {
      console.log(e)
      if (e.length) {
        this.queryParam.start_time = e[0]
        this.queryParam.end_time = e[1]
      } else {
        this.queryParam.start_time = ''
        this.queryParam.end_time = ''
      }
    },
    async getList(pageNo) {
      pageNo && (this.queryParam.pageNo = pageNo)
      const res = await pt.bannerList(this.queryParam)
      this.tabList = res.data.data
      this.total = res.data.totalCount
    },
    // 显示编辑弹框
    showModal(item) {
      if (item) {
        this.form = JSON.parse(JSON.stringify(item))
      } else {
        this.form = this.$options.data().form
      }
      this.visible = true
    },
    onSelectChange(selectedRowKeys) {
      this.selectedRowKeys = selectedRowKeys
    },
    onOk(e) {
      console.log(e)
      this.showLink = false
      this.form.url = e
    },
    async handleOk() {
      if (!this.form.cover_pic) return this.$message.error('图片不能为空')
      this.confirmLoading = true
      const res = await pt.bannerSave(this.form)
      if (this.form.id && res.code == 0) {
        let index = this.tabList.findIndex((item) => item.id == this.form.id)
        this.tabList.splice(index, 1, this.form)
      }
      !this.form.id && this.getList()
      this.visible = false

      this.confirmLoading = false
    },

    // 更多操作
    async handleAll(id, status) {
      if (!id.length) return
      let formData = {
        status,
      }
      if (typeof id == 'string') {
        formData.id = id
      } else {
        formData.ids = id.join(',')
      }
      await pt.bannerSetStatus(formData)
      this.getList()
    },
  },
}
</script>

<style></style>
